﻿@model IEnumerable<Core.Models.Comment>
@{
    var list = Model.ToList();
}
<h4>
    Comments(@Model.Count())</h4>
@for (int i = 0; i < list.Count(); i++)
{
    var item = list[i];
    string cssClass = "";
    if (i % 2 > 0)
    {
        cssClass = "panel-customize-extra-odd";
    }
    else
    {
        cssClass = "panel-customize-extra-even";
    }  
    <div class="panel @cssClass">
        <div class="panel-body">
            <div class="panel-body-createddate">
                @item.FirstName @item.LastName | @Html.DisplayFor(modelItem => item.CreatedDate)
            </div>
            <div class="panel-body-content">
                @Html.DisplayFor(modelItem => item.Content)
            </div>
        </div>
        @if (ViewBag.Authorized == true)
        {
            <div class="panel-body-actionlink">
                <a href="#" class="DeleteLink" data-id="@item.Id">Delete Comment</a>
            </div>
        }
    </div>
}
<script type="text/javascript">
    $(function () {
        $(".DeleteLink").click(function () {
            var recordToDelete = $(this).attr("data-id");
            if (recordToDelete != '') {
                $.ajax({
                    type: "POST", //Phương thức gửi request là POST hoặc GET
                    data: "id=" + recordToDelete, //tham số gửi kèm
                    dataType: 'html', //kiểu dữ liệu trả về, mặc định là text
                    async: false, // NEW
                    url: "/Comment/Delete", //Đường dẫn tới nơi xử lý request ajax
                    beforeSend: function () {
                        console.log("I'm sending AJAX.");
                    },
                    success: function (data) {
                        console.log('response: ' + data);
                    },
                    error: function (er) {
                        console.log(er.responseText);
                    },
                    complete: function (data) {
                        console.log("I'm done.");
                        $('#comment_entries').empty().html(data.responseText);
                    }
                });
            }
        });
    });
</script>
